void __kernel filter_kernel(__global int* image, int imageWidth, int imageHeight, __global int* result,
							float factor, float bias, __global float* filterMatrix, int filterSize)
{
	int index = get_global_id(0);
	
        int x = index % imageWidth;
        int y = index / imageWidth;
	
	float red = 0.0, green = 0.0, blue = 0.0;
	for (int filterX = 0; filterX < filterSize; ++filterX)
	{
		for (int filterY = 0; filterY < filterSize; ++filterY)
		{
                        int imageX = (x - filterSize / 2 + filterX + imageWidth) % imageWidth;
                        int imageY = (y - filterSize / 2 + filterY + imageHeight) % imageHeight;
			
                        int pixel = image[imageY * imageWidth + imageX];
			//int pixel = image[y * imageWidth + x];
			
			red += ((pixel & 0xff0000) >> 16) * filterMatrix[filterY * filterSize + filterX];
			green += ((pixel & 0x00ff00) >> 8) * filterMatrix[filterY * filterSize + filterX];
			blue += (pixel & 0x0000ff) * filterMatrix[filterY * filterSize + filterX];
		}
	}
	
	red = min(max((int)(red * factor + bias), 0), 255);
	green = min(max((int)(green * factor + bias), 0), 255);
	blue = min(max((int)(blue * factor + bias), 0), 255);
	
        int newPixel = ((int)red << 16) | ((int)green << 8) | (int)blue;
        result[y * imageWidth + x] = newPixel;
}
